/*
 * Copyright TLR Team, Inc. All Rights Reserved.
 */
package com.tlr.device.rest;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import static com.tfb.service.utility.AppConst.GET_CLIENT_CRED_TOKEN;
import com.tfb.service.utility.JsonUtil;
import javax.ws.rs.core.MediaType;
import org.codehaus.jackson.JsonNode;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {

    private static String httpAddress;
    private static int proxyPort;
    private static boolean proxyEnabled;
    private static String airMeasureDeviceToken;
    private static String locationCardGroupID;
    private static HikeWebsocketService hikeSocketService;
    private static String hikeWebsocketAddress;
    private static String hikeServerRest;
    private static EbanWebsocketService ebanWebSocketService;
    private static String ebanWebsocketAddress;
    private static String peerNotifyAddress;
    private static String smsServerAddress;
    private static String smsUser;
    private static String smsPwd;
    private static String smsRegisterTemplate;
    private static String docDownloadDir;
    private static long accessTokenTime;
    private static String wechatAccessToken;
    private static int accessTokenExpiredTime;
    private static int ticketExpiredTime;
    private static String wechatTicket;
    private static long ticketTime;
    private static String ebanUser;
    private static String ebanPassword;
    private static String fileUploadPath;
    private static String peerSyncAddress;
    private static ClassPathXmlApplicationContext classPathXmlApplicationContext;

    public static String getPeerSyncAddress() {
        return peerSyncAddress;
    }

    public static void setPeerSyncAddress(String peerSyncAddress) {
        AppContext.peerSyncAddress = peerSyncAddress;
    }

    public static String getFileUploadPath() {
        return fileUploadPath;
    }

    public static void setFileUploadPath(String fileUploadPath) {
        AppContext.fileUploadPath = fileUploadPath;
    }

    public static String getSmsRegisterTemplate() {
        return smsRegisterTemplate;
    }

    public static void setSmsRegisterTemplate(String smsRegisterTemplate) {
        AppContext.smsRegisterTemplate = smsRegisterTemplate;
    }

    public static String getSmsServerAddress() {
        return smsServerAddress;
    }

    public static void setSmsServerAddress(String smsServerAddress) {
        AppContext.smsServerAddress = smsServerAddress;
    }

    public static String getSmsUser() {
        return smsUser;
    }

    public static void setSmsUser(String smsUser) {
        AppContext.smsUser = smsUser;
    }

    public static String getSmsPwd() {
        return smsPwd;
    }

    public static void setSmsPwd(String smsPwd) {
        AppContext.smsPwd = smsPwd;
    }

    public static String getPeerNotifyAddress() {
        return peerNotifyAddress;
    }

    public static void setPeerNotifyAddress(String peerNotifyAddress) {
        AppContext.peerNotifyAddress = peerNotifyAddress;
    }

    public static String getHttpAddress() {
        return httpAddress;
    }

    public static int getProxyPort() {
        return proxyPort;
    }

    public static boolean isProxyEnabled() {
        return proxyEnabled;
    }

    public static void setHttpProxy(String httpAddress, int proxyPort) {
        AppContext.httpAddress = httpAddress;
        AppContext.proxyPort = proxyPort;
    }

    public static void setHttpProxyEnable(boolean proxyEnabled) {
        AppContext.proxyEnabled = proxyEnabled;
    }

    public static void setAirMeasureDeviceToken(String airMeasureDeviceToken) {
        AppContext.airMeasureDeviceToken = airMeasureDeviceToken;
    }

    public static String getAirMeasureDeviceToken() {
        return airMeasureDeviceToken;
    }

    public static void setLocationCardGroupID(String locationCardGroupID) {
        AppContext.locationCardGroupID = locationCardGroupID;
    }

    public static String getLocationCardGroupID() {
        return locationCardGroupID;
    }

    public static void setHikeSocketService(HikeWebsocketService socketService) {
        hikeSocketService = socketService;
    }

    public static HikeWebsocketService getHikeSocketService() {
        return hikeSocketService;
    }

    public static void setHikeWebsocketAddress(String hikeWebsocketAddress) {
        AppContext.hikeWebsocketAddress = hikeWebsocketAddress;
    }

    public static String getHikeWebsocketAddress() {
        return hikeWebsocketAddress;
    }

    public static void setHikeServerRest(String hikeServerRest) {
        AppContext.hikeServerRest = hikeServerRest;
    }

    public static String getHikeServerRest() {
        return hikeServerRest;
    }

    public static void setEbanSocketService(EbanWebsocketService ebanSocketService) {
        ebanWebSocketService = ebanSocketService;
    }

    public static EbanWebsocketService getEbanSocketService() {
        return ebanWebSocketService;
    }

    public static void setEbanSocketAddress(String ebanWebsocketAddress) {
        AppContext.ebanWebsocketAddress = ebanWebsocketAddress;
    }

    public static String getEbanWebsocketAddress() {
        return ebanWebsocketAddress;
    }

    public static void setDocDownloadDir(String strDocDownloadDir) {
        docDownloadDir = strDocDownloadDir;
    }

    public static String getDocDownloadDir() {
        return docDownloadDir;
    }

    public static synchronized String getWechatAccessToken() {
        if (AppContext.wechatAccessToken == null || AppContext.accessTokenTime == 0) {
            return generateAccessToken();
        } else if (AppContext.accessTokenExpiredTime < (System.currentTimeMillis() - AppContext.accessTokenTime) / 1000) {
            return generateAccessToken();
        }
        return AppContext.wechatAccessToken;
    }

    public static synchronized String getTicket() {
        if (AppContext.wechatTicket == null || AppContext.ticketTime == 0) {
            return generateTicket();
        } else if (AppContext.ticketExpiredTime < (System.currentTimeMillis() - AppContext.ticketTime) / 1000) {
            return generateTicket();
        }
        return AppContext.wechatTicket;
    }

    private static String generateTicket() {
        String accessToken = getWechatAccessToken();
        Client client = Client.create();
        WebResource webResource = client.resource(String.format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", accessToken));
        String content = webResource.accept(MediaType.APPLICATION_JSON).get(String.class);
        AppContext.ticketTime = System.currentTimeMillis();
        JsonNode accessTokenNode = JsonUtil.createJson(content);
        AppContext.ticketExpiredTime = accessTokenNode.get("expires_in").asInt();
        AppContext.wechatTicket = accessTokenNode.get("ticket").asText();
        return AppContext.wechatTicket;
    }

    private static String generateAccessToken() {
        Client client = Client.create();
        WebResource webResource = client.resource(GET_CLIENT_CRED_TOKEN);
        String content = webResource.accept(MediaType.APPLICATION_JSON).get(String.class);
        AppContext.accessTokenTime = System.currentTimeMillis();
        JsonNode accessTokenNode = JsonUtil.createJson(content);
        AppContext.accessTokenExpiredTime = accessTokenNode.get("expires_in").asInt();
        AppContext.wechatAccessToken = accessTokenNode.get("access_token").asText();
        return AppContext.wechatAccessToken;
    }

    public static void setEbanDefaultUser(String user) {
        AppContext.ebanUser = user;
    }

    public static void setEbanDefaultPwd(String pwd) {
        AppContext.ebanPassword = pwd;
    }

    public static String getEbanDefaultUser() {
        return ebanUser;
    }

    public static String getEbanDefaultPassword() {
        return ebanPassword;
    }

    public static void setBeanContext(ClassPathXmlApplicationContext classPathXmlApplicationContext) {
        AppContext.classPathXmlApplicationContext = classPathXmlApplicationContext;
    }

    public static ClassPathXmlApplicationContext getBeanContext() {
        return classPathXmlApplicationContext;
    }

}
